import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

enum SearchBarType { home, normal, homeLight }

class SearchBar extends StatefulWidget {
//  是否禁用
  final bool enabled;

//  是否显示左按钮
  final bool hideLeft;

//  限制 当前的搜索框状态
  final SearchBarType searchBartype;

//  提示文字 pleasehoder
  final String hint;

//  搜索框默认值
  final String defaultText;

//  左按钮点击事件
  final void Function() leftButtonClick;

//  右按钮点击事件
  final void Function() rightButtonClick;

//  语音按钮点击事件
  final void Function() speakClick;

//  假input盒子 点击
  final void Function() inputBoxClick;

//  输入框变化 事件 oninput
  final ValueChanged<String> onChanged;

  const SearchBar({Key key,
    this.enabled,
    this.hideLeft,
    this.searchBartype = SearchBarType.normal,
    this.hint = '',
    this.defaultText = '',
    this.leftButtonClick,
    this.rightButtonClick,
    this.speakClick,
    this.inputBoxClick,
    this.onChanged})
      : super(key: key);

  @override
  State<StatefulWidget> createState() => _SearchBarState();
}


class _SearchBarState extends State<SearchBar> {
//  是否显示 x 清除按钮 反之 语音按钮
  bool showClear = false;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    if(widget.defaultText != null) {
      setState(() {
        _controller.text = widget.defaultText;
        showClear = true;
      });
    }
  }
  final TextEditingController _controller = TextEditingController();

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return widget.searchBartype == SearchBarType.normal
        ? _genNormalSearch()
        : _genHomeSearch();
  }

  _genNormalSearch() {
    return Container(
      padding: EdgeInsets.only(top: 26),
      child: Row(
//        mainAxisAlignment: MainAxisAlignment.center,
//        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          _wrapTap(Container(
            padding: EdgeInsets.fromLTRB(10, 4, 10, 4),
            child: widget?.hideLeft ?? false ? null : Icon(Icons.arrow_back),
          ), widget.leftButtonClick),
          Expanded(
            flex: 1,
            child: _inputBox(),
          ),
          _wrapTap(Container(child: Text('搜索',style: TextStyle(color: Colors.blueAccent),),padding: EdgeInsets.all(10),), widget.rightButtonClick)
        ],
      ),
    );
  }

  _genHomeSearch() {
    return Container(
      padding: EdgeInsets.only(top: 26),
      child: Row(
//        mainAxisAlignment: MainAxisAlignment.center,
//        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          _wrapTap(Container(
            padding: EdgeInsets.fromLTRB(10, 4, 10, 4),
            child:Row(children: <Widget>[
              Text('上海',style: TextStyle(color: _homeFontColor()),),
              Icon(Icons.expand_more,color: _homeFontColor(),)
            ],),
          ), widget.leftButtonClick),
          Expanded(
            flex: 1,
            child: _inputBox(),
          ),
          _wrapTap(
              Container(
                margin: EdgeInsets.all(8),
                child: Icon(Icons.message,color: _homeFontColor(),)),
              widget.rightButtonClick)
        ],
      ),
    );
  }

  Widget _inputBox() {
    Color inputBoxColor;
    if (widget.searchBartype == SearchBarType.home) {
      inputBoxColor = Colors.white;
    } else {
      inputBoxColor = Color(0xffEDEDED);
    }
    return Container(
      height: 30,
      padding: EdgeInsets.fromLTRB(10, 4, 10, 4),
      decoration: BoxDecoration(
          color: inputBoxColor,
          borderRadius: BorderRadius.circular(
              widget.searchBartype == SearchBarType.normal ? 5 : 15)
      ),
      child: Row(
//        mainAxisAlignment: MainAxisAlignment.spaceAround,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          Icon(
            Icons.search,
            size: 20,
            color: widget.searchBartype == SearchBarType.normal ?
            Color(0xffA9A9A9) : Colors.lightBlue,
          ),
          Expanded(
            flex: 1,
            child: widget.searchBartype == SearchBarType.normal ?
            TextField(
              controller: _controller,
              onChanged: _onChanged,
              autofocus: true,
              style: TextStyle(
//                backgroundColor: Colors.red,
                  fontSize: 18,
                  color: Colors.black,
                  fontWeight: FontWeight.w300,
              ),
//              textAlign: TextAlign.start,
              decoration: InputDecoration(
                  contentPadding: EdgeInsets.zero,
//                  contentPadding: EdgeInsets.all(1),
                  border: InputBorder.none,
                  hintText: widget.hint ?? '',
                  hintStyle: TextStyle(fontSize: 15),
                            enabledBorder: OutlineInputBorder(
//              /*边角*/
//              borderRadius:BorderRadius.circular(20),
//              borderSide: BorderSide(
//                color: Colors.white, //边线颜色为白色
//                width: 1, //边线宽度为2
//              ),
            ),
            focusedBorder: OutlineInputBorder(
              borderSide: BorderSide(
                color: Colors.transparent, //边框颜色为白色
//                width: 1, //宽度为5
              ),
//              borderRadius:BorderRadius.circular(20),

            ),
              ),
            )
                : _wrapTap(Container(child: Text(widget.defaultText),),
                widget.inputBoxClick),
          ),
          !showClear ?
          _wrapTap(Icon(Icons.mic, size: 22,color: widget.searchBartype == SearchBarType.normal ? Colors.blueAccent:Colors.grey,), widget.speakClick)
              :
          _wrapTap(Icon(Icons.close, color:Colors.grey,size: 22,), () {
            setState(() {
              _controller.clear();
            });
            _onChanged('');
          })

        ],
      ),
    );
  }
  _homeFontColor(){
    return widget.searchBartype == SearchBarType.homeLight?Colors.black54:Colors.white;
  }
  _onChanged(String text) {
    if (text.length > 0) {
      setState(() {
        showClear = true;
      });
      print(text);
    } else {
      setState(() {
        showClear = false;
      });
    }
    if (widget.onChanged != null) {
      widget.onChanged(text);
    }
  }
  _wrapTap(Widget child, void Function() callback) {
    return GestureDetector(
      child: child,
      onTap: () {
        if (callback != null) callback();
      },
    );
  }

}
